
package ch.hearc.coursJava.GUI.j2D.tableAnimerHSB.swings;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import ch.hearc.coursJava.GUI.j2D.tableAnimerHSB.j2d.Rangee;
import ch.hearc.coursJava.GUI.j2D.tableAnimerHSB.settings.AnimationType;
import ch.hearc.coursJava.GUI.j2D.tableAnimerHSB.settings.JPanelDessinSettings;
import ch.hearc.coursJava.GUI.j2D.tableAnimerHSB.settings.Settings;
import ch.hearc.coursJava.GUI.j2D.tools.JPanelDessin_A;

public class JPanelDessin extends JPanelDessin_A
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public JPanelDessin(Settings settings)
		{
		this.settings = settings.getPanelDessinSettings();
		this.rangee = new Rangee(settings);
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	@SuppressWarnings("deprecation")
	public void animerImage(final AnimationType newAnimationType)
		{
		if (settings.getAnimationType() != newAnimationType)
			{
			if (this.animationThread != null)
				{
				this.animationThread.stop();
				}

			settings.setAnimationType(newAnimationType);

			if (settings.getAnimationType() != AnimationType.NONE)
				{
				startAnimation();
				}
			}
		}

	private void startAnimation()
		{
		this.animationThread = new Thread(new Runnable()
			{

				@Override
				public void run()
					{
					while(true)
						{
						switch(settings.getAnimationType())
							{
							case ALPHA:
								this.animerAlpha();
								break;

							case HUE:
								this.animerHue();
								break;

							case HUE_ALPHA:
								this.animerHue();
								this.animerAlpha();
								break;

							case NONE:
								break;
							}

						repaint();
						}
					}

				private void animerAlpha()
					{
					double alpha = settings.getAlpha() + settings.getdAlpha();
					settings.setAlpha(alpha);
					}

				private void animerHue()
					{
					double hue = settings.getHue();

					if (hue >= 1)
						{
						hue = 0;
						}

					hue += settings.getdHue();
					settings.setHue(hue);
					}
			});

		this.animationThread.start();
		}

	public void resetImage()
		{
		this.animerImage(AnimationType.NONE);
		this.settings.setAlpha(0);
		this.settings.setHue(0);
		}

	private void centrerG2d(Graphics2D g2d)
		{
		g2d.translate(this.getSize().width / 2, this.getSize().height / 2);
		}

	@Override
	protected void dessiner(Graphics2D g2d)
		{
		this.centrerG2d(g2d);

		AffineTransform savedTransform = g2d.getTransform();

		for(int i = 1; i <= settings.getNbrRangee(); i++)
			{
			double alphaRangee;

			if (i % 2 == 0)
				{
				alphaRangee = -settings.getAlpha();
				}
			else
				{
				alphaRangee = settings.getAlpha();
				}

			double rayonRangee = i * settings.getdRayon() + settings.getRayon();

			this.rangee.dessiner(g2d, rayonRangee, alphaRangee, settings.getHue());

			g2d.setTransform(savedTransform);
			}
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Input
	private JPanelDessinSettings settings;

	// Tools
	private Thread animationThread;
	private Rangee rangee;

	}
